#include "ZigbeeGWControlPointListCmd.h"

ZigbeeGWControlPointListCmd::ZigbeeGWControlPointListCmd(QObject *parent) :
    BaseCmd(parent)
{
    this->m_protocolType = ProtocolType::ZigbeeControlProtocol;
    this->m_functionCode = FunctionCode;
}

Package ZigbeeGWControlPointListCmd::toPackage()
{
    Package package;
    QByteArray body;
    QByteArray databody;
    quint8 Cmdhand=0x68;
    quint16 Cmdend=0;
//    quint16 Cmdorder=0;

    body.clear();
    body.append(Cmdhand);
    body.append(char(0x01));
    body.append(char(0x00));
    body.append(char(0x01));

//    body.append(m_zigbee.nwk());
//    body.append(Cmdport);

    if(m_zigbee.type()==2)
    {
        qDebug()<<"type right";
    //    body.append(Cmdorder);
        quint8 valueData=0;
        foreach(const ControlPointStruct &controlPoint,m_zigbee.pointList())
       {

            if(m_zigbee.pointValue().size()==0)
            {
              qDebug()<<"==0 value.......................................................";
                switch (controlPoint.value())
                {
                case 0x00:valueData=0x01;

                    break;
                case 0x01:valueData=0x00;

                    break;
                case 0x02:valueData=0x02;

                    break;
                default:valueData=0x02;
                    break;
                }
             }
 //           body.append(valueData);
            QByteArray ZigbeeNwk;
            ZigbeeNwk.append(m_zigbee.nwk().mid(1,1));
            ZigbeeNwk.append(m_zigbee.nwk().mid(0,1));
            qDebug()<<"valuedata="<<(valueData);
            databody.clear();
            databody.append(char(0x00));
            databody.append(char(0xf3));
//            databody.append(m_zigbee.nwk());
            databody.append(ZigbeeNwk);
            databody.append(valueData);
            package.setFunction(FunctionCode);
            break;
        }
    }
    else if(m_zigbee.type()==5)
    {
        qDebug()<<"men suo type right";
//        quint8 valueData=0;
        char keydata[30]={0x00,0x42,0x42,0x14,0xAA,0x0A,0x90,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x31,0x55};
        QByteArray getdata;
        for(int i=0;i<24;i++)
        {
            getdata.append(char(keydata[i]));
        }
    //    body.append(Cmdorder);
//        foreach(const ControlPointStruct &controlPoint,m_zigbee.pointList())
//       {
//        switch (controlPoint.value())
//        {
//        case 0x01:valueData=0x01;break;
//        case 0x00:valueData=0X00;break;
//        }
//       }
        QByteArray ZigbeeNwk;
        ZigbeeNwk.append(m_zigbee.nwk().mid(1,1));
        ZigbeeNwk.append(m_zigbee.nwk().mid(0,1));
        databody.clear();
        databody.append(char(0x70));
        databody.append(char(0x01));
        databody.append(char(0x01));
//        databody.append(m_zigbee.nwk());
        databody.append(ZigbeeNwk);
        databody.append(char(0x10));
        databody.append(char(0x02));
        databody.append(char(0x00));
        databody.append(char(0x00));
        databody.append(getdata.size());
        databody.append(getdata);
      package.setFunction(FunctionCode);
    }
    else if(m_zigbee.type()==6)
    {
          qDebug()<<"Type==6------------------------";
         Zigbee3InfraredXml *zigbee3InfraredXml =Zigbee3InfraredXml::getInstance();
          foreach(const Zigbee3InfraredStruct &InfraredStruct,zigbee3InfraredXml->InfraredList)
          {
              if(InfraredStruct.m_nwk==zigbee().nwk())
              {
                  qDebug()<<"nwk is right now";
                  foreach(const ControlPointStruct &controlPoint,m_zigbee.pointList())
                 {
                      if(controlPoint.value()==InfraredStruct.m_PointValue)
                      {
                          qDebug()<<"Value is right now ";
                  //        QString NWK=InfraredStruct.m_nwk;
                          QByteArray getNewnwk;
                          getNewnwk.append(InfraredStruct.m_nwk.mid(1,1));
                          getNewnwk.append(InfraredStruct.m_nwk.mid(0,1));
                          Package package;
                          package.setBody(InfraredStruct.m_data);
                          package.setFunction(0xD0);
                          QByteArray InfraredData=zigbee3InfraredXml->generateData(package);
                          QByteArray ZBCmd;

//                         ZBCmd.append(0x51);
//                         ZBCmd.append(InfraredStruct.m_nwk);
//                         ZBCmd.append(0x01);
//                         ZBCmd.append((char)0x00);
//                         ZBCmd.append((char)0x00);
//                         ZBCmd.append(0X02);
//                         ZBCmd.append(0x0c);
//                         ZBCmd.append(0X41);
//                         ZBCmd.append(0X42);
//                         ZBCmd.append(InfraredData.size());
//                         ZBCmd.append(InfraredData);
//                         ZBCmd.append(0x15);
                          databody.append(char(0x00));
                          databody.append(char(0xf1));
                          databody.append(getNewnwk);
                       //   databody.append(QByteArray::fromHex(getNewnwk.toLatin1()));
                          databody.append(InfraredData.size());
                          databody.append(InfraredData);
                        //  package.setFunction(FunctionCode);
                      }
                 }

              }
          }

    }
    if(databody.size()<0xff)
    {
        body.append((char)0x00);
    }
    body.append((quint16)databody.size());
    body.append(databody);

    for(int i=0;i<body.size();i++)
    {
        Cmdend+=body.at(i);
    }
    Cmdend=Cmdend&0xff;
    body.append(Cmdend);
    //package.setFunction(FunctionCode);
    package.setBody(body);
    if(databody.size()==0)
    {
        package.clear();
        package.body().clear();
    }
    return package;
}


void ZigbeeGWControlPointListCmd::unPackage(Package package)
{
    Q_UNUSED(package);
}

Zigbee3Struct ZigbeeGWControlPointListCmd::zigbee() const
{
    return m_zigbee;
}

void ZigbeeGWControlPointListCmd::setZigbee(const Zigbee3Struct &zigbee)
{
    m_zigbee = zigbee;
}
